package com.chw.admin.lookup;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chw.admin.order.ChwPoLine;
import com.chw.utils.CommonFieldsUtil;
import com.chw.utils.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class SysLookupValueService implements ISysLookupValueService {

	@Autowired
	private SysLookupValueMapper sysLookupValueMapper;

	@Autowired
	private SysLookupTypeMapper sysLookupTypeMapper;

	// 新增
	@Override
	public int addEntity(SysLookupType sysLookupType, List<SysLookupValue> sysLookupValue) {
		List<SysLookupType> list = sysLookupTypeMapper.findByLookupType(sysLookupType.getLookupType());
		if (null != list && list.size() > 0) {
			// type已经存在
			return -1;
		}
		try {
			sysLookupType.setId(UUIDUtil.getUUID());
			CommonFieldsUtil.setCreateValueByCy(sysLookupType);
			this.sysLookupTypeMapper.insert(sysLookupType);
			if (null != sysLookupValue) {
				if (sysLookupValue.size() > 0) {
					for (int i = 0; i < sysLookupValue.size(); i++) {
						SysLookupValue iSysLookupValue = sysLookupValue.get(i);
						iSysLookupValue.setLookupType(sysLookupType.getLookupType());
						iSysLookupValue.setId(UUIDUtil.getUUID());
						CommonFieldsUtil.setCreateValueByCy(iSysLookupValue);
						this.sysLookupValueMapper.insert(iSysLookupValue);
					}
				}
			}
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
		// try {
		// String id = UUIDUtil.getUUID();
		// entity.setId(id);
		// CommonFieldsUtil.setCreateValueByCy(entity);
		// this.sysLookupValueMapper.insert(entity);
		// return 0;
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// return 1;
	}

	// 修改
	@Override
	public int updateEntity(SysLookupType sysLookupType, List<SysLookupValue> sysLookupValue) {
		// 更新头
		CommonFieldsUtil.setUpdateValue(sysLookupType);
		// sysLookupType.setLastUpdatedDate(new Date());
		// sysLookupType.setEnabledFlag("Y");
		this.sysLookupTypeMapper.updateByPrimaryKeySelective(sysLookupType);
		if (null != sysLookupValue) {
			if (sysLookupValue.size() > 0) {
				// 先物理删除行
				sysLookupValueMapper.deleteByLookupType(sysLookupType.getLookupType());
				// 界面行(包括新增/修改)
				for (int i = 0; i < sysLookupValue.size(); i++) {
					// 界面行
					SysLookupValue iSysLookupValue = sysLookupValue.get(i);
					// 新增
					iSysLookupValue.setId(UUIDUtil.getUUID());
					iSysLookupValue.setLookupType(sysLookupType.getLookupType());
					CommonFieldsUtil.setCreateValueByCy(iSysLookupValue);
					this.sysLookupValueMapper.insert(iSysLookupValue);
				}

				// // 界面行(包括新增/修改)
				// for (int i = 0; i < sysLookupValue.size(); i++) {
				// // 界面行
				// SysLookupValue iSysLookupValue = sysLookupValue.get(i);
				// String lookupValueId = iSysLookupValue.getId();
				// if (null == lookupValueId || "".equals(lookupValueId)) {
				// // 新增
				// iSysLookupValue.setId(UUIDUtil.getUUID());
				// CommonFieldsUtil.setCreateValueByCy(iSysLookupValue);
				// iSysLookupValue.setLookupType(sysLookupType.getLookupType());
				// ;
				// this.sysLookupValueMapper.insert(iSysLookupValue);
				// } else {
				// // 更新
				// CommonFieldsUtil.setUpdateValue(iSysLookupValue);
				// this.sysLookupValueMapper.updateByPrimaryKeySelective(iSysLookupValue);
				// }
				// }
			}
		}
		return 0;
	}
	// try {
	// CommonFieldsUtil.setUpdateValue(entity);
	// this.sysLookupValueMapper.updateByPrimaryKeySelective(entity);
	// return 0;
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// return 1;
	// }

	// 逻辑删除头
	@Override
	public int deleteEntity(String lookupType) {
		try {
			// 失效头
			this.sysLookupTypeMapper.updateByLookupType(lookupType);
			// 失效行
			this.sysLookupValueMapper.updateByLookupType(lookupType);
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	// // 查询 id
	// @Override
	// public SysLookupValue getEntityById(String id) {
	// return this.sysLookupValueMapper.selectByPrimaryKey(id);
	// }

	// 查询 id
	@Override
	public SysLookup getEntityById(String id) {
		SysLookupType sysLookupType = this.sysLookupTypeMapper.selectByPrimaryKey(id);
		SysLookup sysLookup = new SysLookup();
		sysLookup.setSysLookupType(sysLookupType);
		List<SysLookupValue> sysLookupValue = this.sysLookupValueMapper.findByLookupType(sysLookupType.getLookupType());
		if (sysLookupValue.size() > 0) {
			sysLookup.setSysLookupValue(sysLookupValue);
		}
		return sysLookup;
	}

	// 分页查询
	@Override
	public PageInfo<SysLookupValue> findEntity(SysLookupValue entity, Integer currentPage, Integer pageSize) {
		PageHelper.startPage(currentPage.intValue(), pageSize.intValue());
		List<SysLookupValue> list = this.sysLookupValueMapper.findByEntity(entity);
		return new PageInfo(list);
	}

	// 分页查询
	@Override
	public PageInfo<SysLookupType> findEntity(SysLookupType entity, Integer currentPage, Integer pageSize) {
		PageHelper.startPage(currentPage.intValue(), pageSize.intValue());
		List<SysLookupType> list = this.sysLookupTypeMapper.findByEntity(entity);
		return new PageInfo(list);
	}
}